package com.mtz.service.impl;
import com.mtz.dateobject.Answer;
import com.mtz.dateobject.Question;
import com.mtz.enums.DeleteEnum;
import com.mtz.enums.ResultEnum;
import com.mtz.exception.MtzException;
import com.mtz.repository.AnswerRepository;
import com.mtz.repository.QuestionRepository;
import com.mtz.service.AnswerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 煜赜
 * @date 2018/11/28 0028 上午 10:32
 */
@Service
@Slf4j
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerRepository answerRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Override
    public Answer findAnsById(Integer id) {
        if (!answerRepository.findById(id).isPresent()) {
            // 抛出id不存在异常
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        return answerRepository.findById(id).get();
    }

    @Override
    public List<Answer> findAnsByQuestionId(Integer questionId) {
        if (!questionRepository.findById(questionId).isPresent()) {
            // 抛出id不存在异常
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }

        return answerRepository.findByQuestionId(questionId);
    }

    @Override
    public List<Answer> findAnsByAuthor(String author) {
        List<Answer> answerList = answerRepository.findByAuthor(author);

        // 判断是否含有此修改人的答案
        if (answerList.size() == 0) {
            // 抛出无包含此修改人问答的异常
            throw new MtzException(ResultEnum.AUTHOR_NOT_EXIST);
        }
        return answerList;
    }

    @Override
    public List<Answer> findAnsByAngle(Integer angle) {
        List<Answer> answerList = answerRepository.findByAngle(angle);

        // 判断是否含有该视角的问答
        if (answerList.size() == 0) {
            // 抛出无包含此视角问答的异常
            throw new MtzException(ResultEnum.ANGLE_NOT_EXIST);
        }
        return answerList;
    }

    @Override
    public List<Answer> findAnsByIsDeleted(Integer isDeleted) {
        List<Answer> answerList = answerRepository.findByIsDeleted(isDeleted);

        // 判断是否含有该状态信息的问答
        if (answerList.size() == 0) {
            return null;
        }
        return answerList;
    }

    @Override
    public Answer addAnswer(Integer questionId, String answer, String author, Integer angle) {
        Answer newAnswer= new Answer();
        newAnswer.setAnswer(answer);
        newAnswer.setHitCount(0);
        newAnswer.setQuestionId(questionId);
        newAnswer.setAuthor(author);
        newAnswer.setAngle(angle);

        return answerRepository.save(newAnswer);
    }

    @Override
    public void editAnswer(Integer id, String answer, String author, Integer angle) {
        Answer oldAnswer = answerRepository.findById(id).get();
        oldAnswer.setAnswer(answer);
        oldAnswer.setAuthor(author);
        oldAnswer.setAngle(angle);
        answerRepository.save(oldAnswer);
    }

    @Override
    public void updateHitCountByQuesId(Integer questionId) {
        if (!answerRepository.findById(questionId).isPresent()) {
            // 抛出id不存在异常
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Question question = questionRepository.findById(questionId).get();
        Answer answer = answerRepository.findById(question.getAnswerId()).get();
        answer.setHitCount(answer.getHitCount()+1);
        answerRepository.save(answer);
    }

    @Override
    public void updateHitCountByAnsId(Integer id) {
        if (!answerRepository.findById(id).isPresent()) {
            // 抛出id不存在异常
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Answer answer = answerRepository.findById(id).get();
        answer.setHitCount(answer.getHitCount()+1);
        answerRepository.save(answer);
    }

    @Override
    public void publicAnsById(Integer id) {
        if (!answerRepository.findById(id).isPresent()) {
            // 抛出id不存在异常
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Answer answer = answerRepository.findById(id).get();
        answer.setIsDeleted(DeleteEnum.PUBLISH.getCode());
        answerRepository.save(answer);
    }

    @Override
    public void deleteAnsById(Integer id) {
        if (!answerRepository.findById(id).isPresent()) {
            // 抛出id不存在异常
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        Answer answer = answerRepository.findById(id).get();
        answer.setIsDeleted(DeleteEnum.DELETED.getCode());
        answerRepository.save(answer);
    }

    @Override
    public void trueDelete(Integer id) {
        if (!answerRepository.findById(id).isPresent()) {
            // 抛出id不存在异常
            throw new MtzException(ResultEnum.ID_NOT_EXIST);
        }
        answerRepository.deleteById(id);
    }
}
